﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Windows.Input;
using System.Collections.ObjectModel;
using ViewMaker.Core.Controls;

namespace ViewMaker.Core.ViewModels
{
    [View(ViewControlType.Grid, Title = "Dictionary Editor")]
    [ViewProperty(GridViewControl.Properties.Margin, "10")]
#if !SILVERLIGHT
    [ViewProperty(GridViewControl.Properties.WindowWidth, 400)]
    [ViewProperty(GridViewControl.Properties.WindowHeight, 300)]
#else
    [ViewProperty(GridViewControl.Properties.Width, 400)]
    [ViewProperty(GridViewControl.Properties.Height, 300)]
#endif
    [ViewProperty(StackPanelViewControl.Properties.ItemMargin, "0,0,0,5")]
    public class StringDictionayEditViewModel : ViewModelBase
    {
        private const int ButtonWidth = 60;

        public class StringDictionayEditItem : ViewModelBase
        {
            public string Key
            {
                get { return _key; }
                set { _key = value; OnPropertyChanged("Key"); }
            }
            private string _key;

            public string Value
            {
                get { return _value; }
                set { _value = value; OnPropertyChanged("Value"); }
            }
            private string _value;

            
        }


        [Display(Name = "Edit Key")]
        [View(ViewControlType.ComboBox)]
        [ViewProperty(ComboBoxViewControl.Properties.IsEditable, true)]
        [ViewProperty(ComboBoxViewControl.Properties.ItemsSource, "Keys")]
        [ViewProperty(ComboBoxViewControl.Properties.IsEnabled, "IsEditable")]
        public string Key
        {
            get { return SelectedItem == null ? null : SelectedItem.Key; }
            set
            {
                if (SelectedItem != null) SelectedItem.Key = value;
                OnPropertyChanged("Key");
            }
        }

        [Display(Name = "Edit Value")]
        [View(ViewControlType.ComboBox)]
        [ViewProperty(ComboBoxViewControl.Properties.IsEditable, true)]
        [ViewProperty(ComboBoxViewControl.Properties.ItemsSource, "Values")]
        [ViewProperty(ComboBoxViewControl.Properties.IsEnabled, "IsEditable")]
        public string Value
        {
            get { return SelectedItem == null ? null : SelectedItem.Value; }
            set
            {
                if (SelectedItem != null) SelectedItem.Value = value;
                OnPropertyChanged("Value");
            }
        }

        [View(ViewControlType.DataGrid)]
        [ViewProperty(DataGridViewControl.Properties.GridHeight, "*")]
        [ViewProperty(DataGridViewControl.Properties.Height, null)]
        [ViewProperty(DataGridViewControl.Properties.VerticalAlignment, LayoutVerticalAlignment.Stretch)]
        [ViewProperty(DataGridViewControl.Properties.SelectedItem, "SelectedItem")]
        [ViewProperty(DataGridViewControl.Properties.HeaderPosition, LayoutHeaderPosition.Hidden)]
        [ViewProperty(DataGridViewControl.Properties.IsReadOnly, true)]
        public ObservableCollection<StringDictionayEditItem> List { get; set; }


        [Browsable(false)]
        public StringDictionayEditItem SelectedItem
        {
            get { return _selectedItem; }
            set 
            { 
                _selectedItem = value; 
                OnPropertyChanged("SelectedItem");
                OnPropertyChanged("Key");
                OnPropertyChanged("Value");
                OnPropertyChanged("IsEditable");
            }
        }
        private StringDictionayEditItem _selectedItem;


        [Browsable(false)]
        public bool IsEditable
        {
            get { return SelectedItem != null; }
        }

        [Browsable(false)]
        public ObservableCollection<string> Keys { get; set; }

        [Browsable(false)]
        public ObservableCollection<string> Values
        {
            get { return _values; }
            set { _values = value; OnPropertyChanged("Values"); }
        }
        private ObservableCollection<string> _values;

        [Display(Name = "AddNew")]
        [ViewProperty(ButtonViewControl.Properties.Width, ButtonWidth)]
        public ICommand AddNewCommand { get { return CreateCommand(AddNew); } }

        [Display(Name = "Remove")]
        [ViewProperty(ButtonViewControl.Properties.Width, ButtonWidth)]
        public ICommand DeleteCommand { get { return CreateCommand(Delete); } }

        [Display(Name = "Ok")]
        [ViewProperty(ButtonViewControl.Properties.Width, ButtonWidth)]
        public ICommand OkCommand { get { return CreateCommand(Ok); } }

        [Display(Name = "Cancel")]
        [ViewProperty(ButtonViewControl.Properties.Width, ButtonWidth)]
        public ICommand CancelCommand { get { return CreateViewCommand(ViewCommands.Close); } }

        private Dictionary<string, string> originalList { get; set; }

        public StringDictionayEditViewModel(Dictionary<string, string> dict)
        {
            originalList = dict;
            List = new ObservableCollection<StringDictionayEditItem>(dict.Select(x => new StringDictionayEditItem{ Key = x.Key, Value = x.Value}));
            Keys = new ObservableCollection<string>();
            Values = new ObservableCollection<string>();
        }

        private void AddNew()
        {
            var item = new StringDictionayEditItem();
            List.Add(item);
            SelectedItem = item;
        }

        private void Delete()
        {
            if (SelectedItem == null) return;
            List.Remove(SelectedItem);
        }

        private void Ok()
        {
            originalList.Clear();
            foreach (var item in List)
            {
                if (!string.IsNullOrEmpty(item.Key))originalList.Add(item.Key, item.Value);
            }
            ExecuteViewCommand(ViewCommands.Close);
        }

    }

}
